from rest_framework import serializers
from .models import CourseCategory, Teacher, Course, CourseChapter, CourseSection


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ['id', 'name', 'role_name', 'title', 'signature', 'image', 'brief']


# 只用来做序列化
class CourseSerializer(serializers.ModelSerializer):
    teacher = TeacherSerializer()  # 子序列化

    class Meta:
        model = Course
        fields = [
            'id',
            'name',
            'course_img',
            'brief',
            'attachment_path',  # 课题地址
            'pub_sections',  # 发布了多少课时
            'sections',  # 总共有多少
            'price',
            'students',
            'period',  # 建议学习周期

            'course_type_name',  # 课程类型
            'level_name',  # 课程难度等级中文
            'status_name',  # 课程状态中午

            'teacher',  # 重写 返回老师详情
            'section_list',  # 重写的字段拿出最多四个课时，超过四个就返回

        ]


class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields = ['id',
                  'name',
                  'section_type_name',
                  'section_link',
                  'duration',
                  'free_trail']


class CourseChapterSerializer(serializers.ModelSerializer):
        '''
            拿出章节下所有课时，方案有三种
             -表模型中写
             -序列化类中写
             -方式三子序列化

        '''
        # 方式二
        # section_list=serializers.SerializerMethodField()
        # def get_section_list(self,obj):
        #     return []

        # 方式三：子序列化(在章节表中隐藏了通过related_name写的，coursesections这个字段，因为它可以通过章节对象.coursesections)
        coursesections = CourseSectionSerializer(many=True)

        class Meta:
            model = CourseChapter
            fields = ['id', 'name', 'chapter', 'summary', 'coursesections']

